package sk.stuba.fiit.pki.service.impl;

import java.io.IOException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableEntryException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import org.bouncycastle.tsp.TSPException;
import org.bouncycastle.tsp.TimeStampRequest;
import org.bouncycastle.tsp.TimeStampResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sk.stuba.fiit.pki.core.CertificateManager;
import sk.stuba.fiit.pki.core.TimeStampManager;
import sk.stuba.fiit.pki.service.PkiService;
import sk.stuba.fiit.pki.service.SerialService;
import sk.stuba.fiit.pki.service.TimeStampService;

@Service("timeStampService")
public class TimeStampServiceImpl implements TimeStampService{
	
	public TimeStampResponse generateTimeStamp(TimeStampRequest tsRequest,BigInteger serialNumber) throws IOException, IllegalArgumentException, TSPException, GeneralSecurityException {	
		PrivateKey key = CertificateManager.getInstance().loadPrivateKey(PkiService.STORAGE_TYPE,
				PkiService.PATH_FILE_NAME+PkiService.TSA_PATH, PkiService.CERT_PASSWORD);
		X509Certificate certificate = CertificateManager.getInstance().loadCertificate(PkiService.PATH_FILE_NAME+PkiService.TSA_PATH, PkiService.CERT_PASSWORD, PkiService.STORAGE_TYPE);
		return TimeStampManager.getInstance().processTimestampResponse(tsRequest, key, certificate, certificate.getSerialNumber());
	}

}
